﻿import QtQuick
import QtQuick.Window
import QtQuick.Controls


ApplicationWindow {
    id: window
    visible: true
    flags: Qt.Window | Qt.FramelessWindowHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint
    // color: "transparent" // 窗口背景透明

    // 边框宽度
    property int borderWidth: 3

    // 切换最大化/还原
    function toggleMaximized() {
        if (window.visibility === Window.Maximized) {
            window.showNormal();
        } else {
            window.showMaximized();
        }
    }

    // 鼠标区域，用于设置光标形状
    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: {
            const p = Qt.point(mouseX, mouseY);
            const b = borderWidth + 10; // 增加边角区域的大小
            if (p.x < b && p.y < b) return Qt.SizeFDiagCursor;
            if (p.x >= width - b && p.y >= height - b) return Qt.SizeFDiagCursor;
            if (p.x >= width - b && p.y < b) return Qt.SizeBDiagCursor;
            if (p.x < b && p.y >= height - b) return Qt.SizeBDiagCursor;
            if (p.x < b || p.x >= width - b) return Qt.SizeHorCursor;
            if (p.y < b || p.y >= height - b) return Qt.SizeVerCursor;
            return Qt.ArrowCursor;
        }
        acceptedButtons: Qt.NoButton // 不处理实际事件
    }

    // 调整窗口大小
    DragHandler {
        id: resizeHandler
        grabPermissions: TapHandler.TakeOverForbidden
        target: null
        onActiveChanged: if (active) {
            const p = resizeHandler.centroid.position;
            const b = borderWidth + 10; // 增加边角区域的大小
            let edges = 0;
            if (p.x < b) { edges |= Qt.LeftEdge }
            if (p.x >= width - b) { edges |= Qt.RightEdge }
            if (p.y < b) { edges |= Qt.TopEdge }
            if (p.y >= height - b) { edges |= Qt.BottomEdge }
            window.startSystemResize(edges);
        }
    }

    // 工具栏（用于拖动窗口）
    ToolBar {
        id: toolBar
        width: parent.width - 2 * borderWidth
        height: 80
        x: borderWidth; y: borderWidth
        background: Rectangle {
            color: "transparent" // 设置 ToolBar 背景为透明
        }

        Item {
            anchors.fill: parent

            // 双击最大化/还原
            TapHandler {
                onTapped: if (tapCount === 2) toggleMaximized()
                gesturePolicy: TapHandler.DragThreshold
            }

            // 拖动窗口
            DragHandler {
                grabPermissions: TapHandler.CanTakeOverFromAnything
                onActiveChanged: if (active) { window.startSystemMove(); }
            }
        }
    }
}
